<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-caret-right"></i> 个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="user" label-width="130px" class="demo-ruleForm">
      <el-form-item label="请输入旧密码" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword"></el-input>
      </el-form-item>
      <el-form-item label="请输入新密码" prop="newPassword">
        <el-input v-model="ruleForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item style="margin: 40px 50px">
        <el-button type="primary" @click="submitForm('user')">确认</el-button>
        <el-button @click="resetForm('user')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        oldPassword: '',
        newPassword: ''
      },
      rules: {
        oldPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        newPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    submitForm(user) {
      this.$refs[user].validate((valid) => {
          let url = 'http://localhost:8080/user/updatePassword';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {'arrayFormat': 'repeat'});
          console.log('formData = ' + formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：');
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '修改成功,请重新登录',
                type: 'success'
              });
              location.href='/userlogin';
            } else {
              this.$message.error(responseBody.message);
            }
          });
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {

  }
}
</script>

<style scoped>
form {
  display: block;
  margin: 70px 300px 0 0;
}
</style>